package com.example.first_android_project;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Intent i;
	static final int CODE_RETOUR_ACTIVITY2 = 2;
	static final int CODE_RETOUR_ACTIVITY4 = 4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button act1=(Button) findViewById(R.id.act1);
		Button act2=(Button) findViewById(R.id.act2);
		Button act3=(Button) findViewById(R.id.act3);
		Button act4=(Button) findViewById(R.id.act4);
		act1.setOnClickListener(btnclick);
		act2.setOnClickListener(btnclick);
		act3.setOnClickListener(btnclick);
		act4.setOnClickListener(btnclick);
		SharedPreferences myPrefs = this.getSharedPreferences("mesvariablesglobales",
				0);
				SharedPreferences.Editor prefsEditor = myPrefs.edit();
				prefsEditor.putString("svar1", "Bonjour");
				prefsEditor.putInt("ivar2", 2);
				prefsEditor.commit();
	}
	private OnClickListener btnclick = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.act1:
				i = new Intent(getApplicationContext(), Act1.class);
				i.putExtra("param1", "Démarrage activité : ");
				i.putExtra("param2", 1);
				startActivity(i);
				break;
			case R.id.act2:
				i = new Intent(getApplicationContext(), Act2.class);
				startActivityForResult(i, CODE_RETOUR_ACTIVITY2);
				break;
			case R.id.act3:
				i = new Intent(getApplicationContext(), Act3.class);
				startActivityForResult(i, CODE_RETOUR_ACTIVITY4);
				break;
			case R.id.act4:
				i = new Intent(getApplicationContext(), Act4.class);
				startActivity(i); 
				break;
			}
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// on regarde quelle Activity a répondu
		switch (requestCode) {
		case CODE_RETOUR_ACTIVITY2:
			switch (resultCode) {
			case Activity.RESULT_OK:
				Toast.makeText(getApplicationContext(), "retour act2 ok",Toast.LENGTH_LONG).show(); 
						return;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getApplicationContext(), "retour act2 cancel",Toast.LENGTH_LONG).show();
							return;
			}
		case CODE_RETOUR_ACTIVITY4:
			switch (resultCode) {
			case Activity.RESULT_OK:
				Bundle b = data.getExtras();
				String string_1 = b.getString("string_1","");
				String string_2 = b.getString ("string_2","");
				int int_1 = b.getInt("int_1", 0);
				long long_1 = b.getLong ("long_1", 0);
				Toast.makeText(getApplicationContext(), "retour act4 ok : Nom:" +
						string_1+"\n prenom "+string_2 +" \n Age"+Integer.toString(int_1)+"\n Numéro Fétiche"+Long.toString(long_1),Toast.LENGTH_LONG).show();
						return;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getApplicationContext(), "retour act4 cancel",Toast.LENGTH_LONG).show();
							return;
			}
		}
	}
}
